<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="栏目id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <j-dict-select-tag type="list" v-model="model.categoryId" dictCode="temple_activity_category,name,id" placeholder="请选择栏目id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="img">
              <imag-cropper v-model="model.img" :width="200" :height="150"></imag-cropper>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginTime">
              <j-date placeholder="请选择开始时间"  v-model="model.beginTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择结束时间"  v-model="model.endTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="location">
              <a-input v-model="model.location" placeholder="请输入地点"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isLink">
              <j-switch v-model="model.isLink"  ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.isLink=='Y'">
            <a-form-model-item label="链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <a-textarea v-model="model.url" rows="4" placeholder="请输入链接" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.isLink=='N'">
            <a-form-model-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <j-editor v-model="model.content" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报名开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signBeginDate">
              <j-date placeholder="请选择报名开始日期" v-model="model.signBeginDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报名结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signEndDate">
              <j-date placeholder="请选择报名结束日期" v-model="model.signEndDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报名电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入报名电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报名费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signFee">
              <a-input v-model="model.signFee" placeholder="请输入报名费"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import ImagCropper from '../../../jeecg/ImagCropper'
  import store from '@/store'


  export default {
    name: 'TempleActivityForm',
    components: {
      ImagCropper
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           categoryId: [
              { required: true, message: '请输入栏目id!'},
           ],
           title: [
              { required: true, message: '请输入标题!'},
           ],
           img: [
              { required: true, message: '请输入图片!'},
           ],
           beginTime: [
              { required: true, message: '请输入开始时间!'},
           ],
           endTime: [
              { required: true, message: '请输入结束时间!'},
           ],
           location: [
              { required: true, message: '请输入地点!'},
           ],
           signBeginDate: [
              { required: true, message: '请输入报名开始日期!'},
           ],
           signEndDate: [
              { required: true, message: '请输入报名结束日期!'},
           ],
           phone: [
              { required: true, message: '请输入报名电话!'},
           ],
           signFee: [
              { required: true, message: '请输入报名费!'},
           ],
        },
        url: {
          add: "/temple.activity/templeActivity/add",
          edit: "/temple.activity/templeActivity/edit",
          queryById: "/temple.activity/templeActivity/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>